package com.sphd.education.activitys;

import android.Manifest;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.sphd.education.EducationApplication;
import com.sphd.education.R;
import com.sphd.education.base.BaseActivity;
import com.sphd.education.info.CityGpsInfo;
import com.sphd.education.info.EducationIntentKey;
import com.sphd.education.info.HistoryInfo;
import com.sphd.education.info.LessonInfo;
import com.sphd.education.info.MyInfo;
import com.sphd.education.info.SchoolImgInfo;
import com.sphd.education.info.SchoolInfo;
import com.sphd.education.info.SchoolTeacherImgInfo;
import com.sphd.education.info.ScoreInfo;
import com.sphd.education.modle.EducationUrl;
import com.sphd.education.utils.ToastUtils;
import com.sphd.education.view.materialfavoritebutton.MaterialFavoriteButton;
import com.sphd.education.view.tagview.TagCloudView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 学校简介
* */
public class SchoolInfoActivity extends BaseActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.school_image_card)
    CardView schoolImageCard;
    @Bind(R.id.schoolteacher_image_card)
    CardView schoolteacherImageCard;//学校老师的图片集
    @Bind(R.id.school_img)
    ImageView schoolImg;
    @Bind(R.id.school_img_1)
    ImageView schoolImg1;
    @Bind(R.id.school_img_2)
    ImageView schoolImg2;
    @Bind(R.id.school_img_3)
    ImageView schoolImg3;
    @Bind(R.id.school_tv_1)
    TextView tv1;
    @Bind(R.id.school_tv_2)
    TextView tv2;
    @Bind(R.id.school_tv_3)
    TextView tv3;
    @Bind(R.id.teacher_tv1)
    TextView tch1;
    @Bind(R.id.teacher_tv2)
    TextView tch2;
    @Bind(R.id.teacher_tv3)
    TextView tch3;
    @Bind(R.id.school_tv1)
    TextView scl1;
    @Bind(R.id.school_tv2)
    TextView scl2;
    @Bind(R.id.school_tv3)
    TextView scl3;


    @Bind(R.id.teacher_img_1)
    ImageView teacherImg1;
    @Bind(R.id.teacher_img_2)
    ImageView teacherImg2;
    @Bind(R.id.teacher_img_3)
    ImageView teacherImg3;
    @Bind(R.id.tag_cloud_view_interestlesson)
    TagCloudView tagCloudViewInterestlesson;
    @Bind(R.id.school_name_textview)
    TextView schoolName;
    @Bind(R.id.school_study_tpye)
    TextView studyOrientation;
    @Bind(R.id.guanggao_textview)
    TextView guanggaoTextview;
    @Bind(R.id.school_title_fen_textview)
    TextView schoolFen;
    @Bind(R.id.details_xiangqing_textview)
    TextView detailsXiangqing;
    @Bind(R.id.relation_phone)
    TextView relationPhone;
    @Bind(R.id.relation_address)
    TextView relationAddress;
    @Bind(R.id.imge_jiaoxue)
    ImageView imageJiaoxue;
    @Bind(R.id.score_jiaoxue)
    TextView scoreJiaoxue;
    @Bind(R.id.image_school_huanjing)
    ImageView schooleHuanjing;
    @Bind(R.id.score_school_huanjing)
    TextView scoreHuanjing;
    @Bind(R.id.image_time)
    ImageView imageTime;
    @Bind(R.id.score_time)
    TextView scoreTime;
    private String cityName;
    private MaterialFavoriteButton toolbarFavorite;
    private Intent intent;
    private Context context;
    private Bundle bundle;
    private SchoolInfo schoolInfo;
    private String flag;
    private int school_id;
    private int collect;
    private MyInfo myInfo;
    boolean favorite = false;
    String phoneno = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_school_info);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initView();

    }

    @Override
    public void initView() {
        myInfo = DataSupport.findLast(MyInfo.class);
        intent = getIntent();
        context = SchoolInfoActivity.this;
        bundle = intent.getBundleExtra(EducationIntentKey.SCHOOL);
        flag = bundle.getString(EducationIntentKey.FLAG);
        school_id = bundle.getInt(EducationIntentKey.SCHOOL_INFO);
        schoolInfo = new SchoolInfo();
        //加载网络数据
        getSchooleInfo(school_id);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//这是自带的箭头的返回的监听
            }
        });
    }

    private void getSchooleInfo(final int school_id) {
        showMyDialog();
        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                EducationUrl.HttpTitle + EducationUrl.SchoolInfo,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        getJsonToSchool(response, school_id);
                        Log.e("response", response);
                    }
                }
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                EducationUrl.showToast(context, error.toString());
                dismissMyDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("oid", school_id + "");
                if (myInfo.getUserid() != 0 && myInfo != null) {
                    map.put("memberId", "" + myInfo.getUserid());
                    Log.e("oid", school_id + "");
                    Log.e("memberId", myInfo.getUserid() + "");
                }
                return map;
            }
        };
        stringRequest.setTag(EducationIntentKey.SCHOOL_INFO);
        EducationApplication.getRequestQueue().add(stringRequest);
        EducationApplication.getRequestQueue().start();
    }

    private void getJsonToSchool(String string, int school_id) {
        dismissMyDialog();
        try {
            JSONObject jsonObject = new JSONArray(string).getJSONObject(0);
            if (jsonObject.has("orgImageation1") && !jsonObject.getString("orgImageation1").equals("[]")) {
                Gson gson = new Gson();
                String[][] dataMap = gson.fromJson(jsonObject.get("orgImageation1").toString(), String[][].class);
                schoolInfo.getSchoolImgInfoList().clear();
                for (int i = 0; i < dataMap.length; i++) {
                    SchoolImgInfo schoolImgInfo = new SchoolImgInfo();
                    schoolImgInfo.setImg_name(dataMap[i][0]);
                    EducationUrl.showLog(dataMap[i][1]);
                    schoolImgInfo.setSchool_img_url(dataMap[i][1]);
                    schoolImgInfo.setIntroduce(dataMap[i][2]);
                    schoolImgInfo.setSchool_id(school_id);
                    schoolInfo.getSchoolImgInfoList().add(schoolImgInfo);
                }
                EducationUrl.showLog(dataMap.length + "dataMap");
            }
            if (jsonObject.has("organization1")) {//解析学校详情
                JSONObject schoolJson = new JSONObject(jsonObject.getString("organization1"));
                if (schoolJson.has("id")) {
                    schoolInfo.setSchool_id(schoolJson.getInt("id"));
                }
                if (schoolJson.has("address")) {
                    relationAddress.setText("上课地址:" + schoolJson.getString("address"));
                }
                if (schoolJson.has("description")) {
                    detailsXiangqing.setText(schoolJson.getString("description"));
                }
                if (schoolJson.has("img")) {
                    ImageLoader.getInstance().displayImage(EducationUrl.HttpTitle + "/" + schoolJson.getString("img"), schoolImg, setSchoolImg());
                }
                if (schoolJson.has("name"))
                    schoolName.setText(schoolJson.getString("name"));
                if (schoolJson.has("trainingDirection")) {
                    studyOrientation.setText("培训方向：" + schoolJson.getString("trainingDirection"));
                }
                if (schoolJson.has("phone")) {
                    relationPhone.setText("热线电话" + schoolJson.getString("phone"));
                    phoneno = schoolJson.getString("phone");
                }
                if (schoolJson.has("ads")) {
                    guanggaoTextview.setText(schoolJson.getString("ads"));
                }
                if (schoolJson.has("score")) {
                    schoolFen.setText(schoolJson.getString("score"));
                }
                if (schoolJson.has("flag")) {
                    collect = schoolJson.getInt("flag");
                    Log.e("collect", collect + "");
                    if (collect == 0) {
                        toolbarFavorite.setFavorite(false);
                        favorite = false;
                    } else {
                        toolbarFavorite.setFavorite(true);
                        favorite = true;
                    }
                }
            }
            if (jsonObject.has("educationClass1")) {//解析对应课程
                JSONArray jsonArray = new JSONArray(jsonObject.getString("educationClass1"));
                schoolInfo.getLessonInfoList().clear();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject lessonJson = jsonArray.getJSONObject(i);
                    LessonInfo lessonInfo = new LessonInfo();
                    if (lessonJson.has("id"))
                        lessonInfo.setLesson_id(lessonJson.getInt("id"));
                    if (lessonJson.has("lessonName"))
                        lessonInfo.setName(lessonJson.getString("lessonName"));
                    if (lessonJson.has("image"))
                        lessonInfo.setImage(lessonJson.getString("image"));
                    if (lessonJson.has("description"))
                        lessonInfo.setDescription(lessonJson.getString("description"));
                    lessonInfo.setSchoolid(school_id);

                    schoolInfo.getLessonInfoList().add(lessonInfo);
                }
            }
            if (jsonObject.has("teacherRecommends1")) {
                Gson gson = new Gson();
                EducationUrl.showLog(jsonObject.getString("teacherRecommends1"));
                String[][] dataMap = gson.fromJson(jsonObject.getString("teacherRecommends1"), String[][].class);
                schoolInfo.getSchoolTeacherImgInfoList().clear();
                for (int i = 0; i < dataMap.length; i++) {
                    SchoolTeacherImgInfo schoolTeacherImgInfo = new SchoolTeacherImgInfo();
                    if (!TextUtils.isEmpty(dataMap[i][0]))
                        schoolTeacherImgInfo.setTeacher_name(dataMap[i][0]);
                    if (!TextUtils.isEmpty(dataMap[i][1]))
                        schoolTeacherImgInfo.setTeacher_img_url(dataMap[i][1]);
                    if (!TextUtils.isEmpty(dataMap[i][2]))
                        schoolTeacherImgInfo.setTchSchoolName(dataMap[i][2]);
                    if (!TextUtils.isEmpty(dataMap[i][3])) {
                        schoolTeacherImgInfo.setTchage(Integer.parseInt(dataMap[i][3]));
                    } else {
                        schoolTeacherImgInfo.setTchage(0);
                    }
                    if (!TextUtils.isEmpty(dataMap[i][4]))
                        schoolTeacherImgInfo.setSubject(dataMap[i][4]);
                    if (!TextUtils.isEmpty(dataMap[i][5]))
                        schoolTeacherImgInfo.setIntroduce(dataMap[i][5]);
                    schoolTeacherImgInfo.setSchool_id(school_id);
                    schoolInfo.getSchoolTeacherImgInfoList().add(schoolTeacherImgInfo);
                }

            }
            if (jsonObject.has("studentFeedbacks")) {
                Gson gson = new Gson();
                float[][] dataMap = gson.fromJson(jsonObject.getString("studentFeedbacks"), float[][].class);
                for (int i = 0; i < dataMap.length; i++) {
                    if (!String.valueOf(dataMap[i][0]).equals(null)) {
                        imageJiaoxue.setImageResource(ids(dataMap[i][0]));
                        scoreJiaoxue.setText(dataMap[i][0] + "分");
                    }
                    if (!String.valueOf(dataMap[i][1]).equals(null)) {
                        schooleHuanjing.setImageResource(ids(dataMap[i][1]));
                        scoreHuanjing.setText(dataMap[i][1] + "分");
                    }
                    if (!String.valueOf(dataMap[i][1]).equals(null)) {
                        imageTime.setImageResource(ids(dataMap[i][2]));
                        scoreTime.setText(dataMap[i][2] + "分");
                    }
                }
            } else {
                imageJiaoxue.setImageResource(ids(5.0));
                scoreJiaoxue.setText(5.0 + "分");
                schooleHuanjing.setImageResource(ids(5.0));
                scoreHuanjing.setText(5.0 + "分");
                imageTime.setImageResource(ids(5.0));
                scoreTime.setText(5.0 + "分");
            }
            findViewById();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void findViewById() {
        initData();
        interestCard();
    }

    private void initData() {
        if (schoolInfo.getSchoolImgInfoList().size() != 0) {
            if (schoolInfo.getSchoolImgInfoList().size() >= 1) {
                if (!TextUtils.isEmpty(schoolInfo.getSchoolImgInfoList().get(0).getSchool_img_url())) {
                    ImageLoader.getInstance().displayImage(EducationUrl.HttpTitle + "/" + schoolInfo.getSchoolImgInfoList().get(0).getSchool_img_url(), schoolImg1);
                    Log.e("图1", EducationUrl.HttpTitle + "/" + schoolInfo.getSchoolImgInfoList().get(0).getSchool_img_url());
                }
                if (!TextUtils.isEmpty(schoolInfo.getSchoolImgInfoList().get(0).getImg_name())) {
                    tv1.setText(schoolInfo.getSchoolImgInfoList().get(0).getImg_name());
                }
            }
            if (schoolInfo.getSchoolImgInfoList().size() >= 2) {
                if (!TextUtils.isEmpty(schoolInfo.getSchoolImgInfoList().get(1).getSchool_img_url())) {
                    ImageLoader.getInstance().displayImage(EducationUrl.HttpTitle + "/" + schoolInfo.getSchoolImgInfoList().get(1).getSchool_img_url(), schoolImg2);
                    Log.e("图2", EducationUrl.HttpTitle + "/" + schoolInfo.getSchoolImgInfoList().get(1).getSchool_img_url());
                }
                if (!TextUtils.isEmpty(schoolInfo.getSchoolImgInfoList().get(1).getImg_name())) {
                    tv2.setText(schoolInfo.getSchoolImgInfoList().get(1).getImg_name());
                }
            }
            if (schoolInfo.getSchoolImgInfoList().size() >= 3) {
                if (!TextUtils.isEmpty(schoolInfo.getSchoolImgInfoList().get(2).getSchool_img_url())) {
                    ImageLoader.getInstance().displayImage(EducationUrl.HttpTitle + "/" + schoolInfo.getSchoolImgInfoList().get(2).getSchool_img_url(), schoolImg3);
                    Log.e("图3", EducationUrl.HttpTitle + "/" + schoolInfo.getSchoolImgInfoList().get(2).getSchool_img_url());
                }
                if (!TextUtils.isEmpty(schoolInfo.getSchoolImgInfoList().get(2).getImg_name())) {
                    tv3.setText(schoolInfo.getSchoolImgInfoList().get(2).getImg_name());
                }
            }
        }

        if (schoolInfo.getSchoolTeacherImgInfoList().size() != 0) {
            if (schoolInfo.getSchoolTeacherImgInfoList().size() >= 1) {
                if (!TextUtils.isEmpty(schoolInfo.getSchoolTeacherImgInfoList().get(0).getTeacher_img_url())) {
                    ImageLoader.getInstance().displayImage(EducationUrl.HttpTitle + "/" + schoolInfo.getSchoolTeacherImgInfoList().get(0).getTeacher_img_url(), teacherImg1);
                }
                if (!TextUtils.isEmpty(schoolInfo.getSchoolTeacherImgInfoList().get(0).getTeacher_name())) {
                    tch1.setText(schoolInfo.getSchoolTeacherImgInfoList().get(0).getTeacher_name());
                }
                if (!TextUtils.isEmpty(schoolInfo.getSchoolTeacherImgInfoList().get(0).getTchSchoolName())) {
                    scl1.setText(schoolInfo.getSchoolTeacherImgInfoList().get(0).getTchSchoolName());
                }
            }
            if (schoolInfo.getSchoolTeacherImgInfoList().size() >= 2) {
                if (!TextUtils.isEmpty(schoolInfo.getSchoolTeacherImgInfoList().get(1).getTeacher_img_url())) {
                    ImageLoader.getInstance().displayImage(EducationUrl.HttpTitle + "/" + schoolInfo.getSchoolTeacherImgInfoList().get(1).getTeacher_img_url(), teacherImg2);
                }
                if (!TextUtils.isEmpty(schoolInfo.getSchoolTeacherImgInfoList().get(1).getTeacher_name())) {
                    tch2.setText(schoolInfo.getSchoolTeacherImgInfoList().get(1).getTeacher_name());
                }
                if (!TextUtils.isEmpty(schoolInfo.getSchoolTeacherImgInfoList().get(1).getTchSchoolName())) {
                    scl2.setText(schoolInfo.getSchoolTeacherImgInfoList().get(1).getTchSchoolName());
                }
            }
            if (schoolInfo.getSchoolTeacherImgInfoList().size() >= 3) {
                if (!TextUtils.isEmpty(schoolInfo.getSchoolTeacherImgInfoList().get(2).getTeacher_img_url())) {
                    ImageLoader.getInstance().displayImage(EducationUrl.HttpTitle + "/" + schoolInfo.getSchoolTeacherImgInfoList().get(2).getTeacher_img_url(), teacherImg3);
                }
                if (!TextUtils.isEmpty(schoolInfo.getSchoolTeacherImgInfoList().get(2).getTeacher_name())) {
                    tch3.setText(schoolInfo.getSchoolTeacherImgInfoList().get(2).getTeacher_name());
                }
                if (!TextUtils.isEmpty(schoolInfo.getSchoolTeacherImgInfoList().get(2).getTchSchoolName())) {
                    scl3.setText(schoolInfo.getSchoolTeacherImgInfoList().get(2).getTchSchoolName());
                }
            }
        }
    }

    private int ids(double a) {
        if (a > 0 && a <= 0.5) {
            return R.mipmap.xingxing0_5;
        } else if (a > 0.5 && a <= 1.0) {
            return R.mipmap.xingxing1;
        } else if (a > 1 && a <= 1.5) {
            return R.mipmap.xingxing1_5;
        } else if (a > 1.5 && a <= 2.0) {
            return R.mipmap.xingxing2;
        } else if (a > 2 && a <= 2.5) {
            return R.mipmap.xingxing2_5;
        } else if (a > 2.5 && a <= 3.0) {
            return R.mipmap.xingxing3;
        } else if (a > 3 && a <= 3.5) {
            return R.mipmap.xingxing3_5;
        } else if (a > 3.5 && a <= 4.0) {
            return R.mipmap.xingxing4;
        } else if (a > 4.0 && a <= 4.5) {
            return R.mipmap.xingxing4_5;
        } else if (a > 4.5 && a <= 5.0) {
            return R.mipmap.xingxing5;
        }
        return 0;
    }

    @Override
    public void myNet() {
    }

    private void interestCard() {
        List<String> tags = new ArrayList<>();
        for (int i = 0; i < schoolInfo.getLessonInfoList().size(); i++) {
            tags.add(schoolInfo.getLessonInfoList().get(i).getName());
        }
        tagCloudViewInterestlesson.setTags(tags);
        tagCloudViewInterestlesson.setOnTagClickListener(new TagCloudView.OnTagClickListener() {
            @Override
            public void onTagClick(int position) {
                if (position == -1) {
                    tagCloudViewInterestlesson.singleLine(false);
                    Toast.makeText(getApplicationContext(), "点击展开标签",
                            Toast.LENGTH_SHORT).show();
                } else {
                    //跳转到对应的课程
//                    getLessonInfo(schoolInfo.getLessonInfoList().get(position).getLesson_id());

                    bundle.putInt("lessonId", schoolInfo.getLessonInfoList().get(position).getLesson_id());
                    bundle.putString(EducationIntentKey.FLAG, flag);
                    intent.putExtra(EducationIntentKey.LESSON, bundle);
                    intent.setClass(context, LessonInfoActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_shouchang, menu);
        toolbarFavorite = (MaterialFavoriteButton) MenuItemCompat.getActionView(menu
                .findItem(R.id.menu_shouchang));
        toolbarFavorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (myInfo == null || myInfo.getUserid() == 0) {
                    toolbarFavorite.setFavorite(false);
                    favorite = false;
                    EducationUrl.showToast(context, "尚未登录");
                    intent.setClass(context, LoginActivity.class);
                    startActivityForResult(intent, 1);
                } else {
                    collectSchool();//收藏 或者取消
                }
            }
        });
        return true;
    }

    private void collectSchool() {
        final StringRequest stringRequest = new StringRequest(Request.Method.POST,
                EducationUrl.HttpTitle + EducationUrl.Collect,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        switch (response) {

                            case "yes":
                                EducationUrl.showToast(context, "收藏成功");
                                toolbarFavorite.setFavorite(true);
                                favorite = true;
                                break;
                            case "re":
                                EducationUrl.showToast(context, "取消收藏");
                                toolbarFavorite.setFavorite(false);
                                favorite = false;
                                break;
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("memberId", myInfo.getUserid() + "");
                map.put("type", 1 + "");
                map.put("collectID", schoolInfo.getSchool_id() + "");
                Log.e("学校的id", schoolInfo.getSchool_id() + "");
                if (favorite) {
                    map.put("cType", 0 + "");
                } else {
                    map.put("cType", 1 + "");
                }
                return map;
            }
        };
        stringRequest.setTag(EducationIntentKey.COLLECT_LESSON_INFO);
        EducationApplication.getRequestQueue().add(stringRequest);
        EducationApplication.getRequestQueue().start();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.menu_shouchang) {
            return true;
        }else if (id==R.id.menu_fenxaing){
            GetandSaveCurrentImage();
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == EducationIntentKey.LOGONCODE) {
                Log.e("onActivityResult", "onActivityResult");
                myInfo = DataSupport.findLast(MyInfo.class);
                getSchooleInfo(school_id);
            }
        }
    }

    //跳转到学校教师的图片集合
    @OnClick(R.id.schoolteacher_image_card)
    public void getSchoolTeacherImage() {
        bundle.putSerializable(EducationIntentKey.SEARCH_LESSON_IMAGE, schoolInfo);
        bundle.putString("type", EducationIntentKey.SEARCH_LESSON_IMAGE);
        intent = new Intent(context, ImageShowActivity.class);
        intent.putExtra(EducationIntentKey.FLAG, bundle);
        startActivity(intent);
    }

    @OnClick(R.id.relation_phone_img)
    public void callTel() {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneno));
        startActivity(intent);
    }

    @OnClick(R.id.school_image_card)
    public void getSchoolImage() {
        bundle.putSerializable(EducationIntentKey.SEARCH_SCHOOL_IMAGE, schoolInfo);
        bundle.putString("type", EducationIntentKey.SEARCH_SCHOOL_IMAGE);
        intent = new Intent(context, ImageShowActivity.class);
        intent.putExtra(EducationIntentKey.FLAG, bundle);
        startActivity(intent);
    }

    private void GetandSaveCurrentImage(){
        //1.构建Bitmap
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int w = display.getWidth();
        int h = display.getHeight();

        Bitmap Bmp = Bitmap.createBitmap( w, h, Bitmap.Config.ARGB_8888 );

        //2.获取屏幕
        View decorview = this.getWindow().getDecorView();
        decorview.setDrawingCacheEnabled(true);
        Bmp = decorview.getDrawingCache();

        String SavePath = getSDCardPath()+"/MDeducation/ScreenImage";

        //3.保存Bitmap
        try {
            File path = new File(SavePath);
            //文件
            String filepath = SavePath + "/Screen_1.png";
            File file = new File(filepath);
            if(!path.exists()){
                path.mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }

            FileOutputStream fos = null;
            fos = new FileOutputStream(file);
            if (null != fos) {
                Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
                //由文件得到uri
                Uri imageUri = Uri.fromFile(new File(filepath));
                Log.d("share", "uri:" + imageUri);  //输出：file:///storage/emulated/0/test.jpg
                Intent shareIntent = new Intent();
                shareIntent.setAction(Intent.ACTION_SEND);
                shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
                shareIntent.setType("image/*");
                startActivity(Intent.createChooser(shareIntent, "分享到"));
                Toast.makeText(getApplicationContext(), "截屏文件已保存至SDCard/AndyDemo/ScreenImage/下", Toast.LENGTH_LONG).show();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取SDCard的目录路径功能
     * @return
     */
    private String getSDCardPath(){
        File sdcardDir = null;
        //判断SDCard是否存在
        boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
        if(sdcardExist){
            sdcardDir = Environment.getExternalStorageDirectory();
        }
        return sdcardDir.toString();
    }
}
